
package com.dyzx.hbb.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Spring MVC 配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 配置路径匹配
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 添加统一的API前缀，但排除Swagger相关路径
      configurer.addPathPrefix("/", c -> {
            if (c == null || c.getPackageName() == null) {
                return false;
            }
            
            String packageName = c.getPackageName();
            
            // 排除Swagger和SpringDoc相关路径
            if (packageName.startsWith("org.springdoc") ||
                packageName.startsWith("springfox.documentation") ||
                packageName.startsWith("io.swagger") ||
                packageName.contains("swagger") ||
                packageName.contains("springdoc")) {
                return false;
            }
            
            // 只对com.kuzlak包下的类添加前缀
            return packageName.startsWith("com.dyzx.hbb");
        });
    }

    /**
     * 配置内容协商
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
     //   configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")    // 允许跨域访问的路径
                .allowedOrigins("*")    // 允许跨域访问的源
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")    // 允许请求方法
                .maxAge(168000)    // 预检间隔时间
                .allowedHeaders("*")  // 允许头部设置
                .allowCredentials(true);    // 是否发送cookie
        registry.addMapping("/**");


        //添加映射路径
       /* //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addMapping("/static/**",ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
        registry.addResourceHandler("/.well-known/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/.well-known/");*/
   /*     registry.addMapping("/**")
                //放行哪些原始域
                .allowedOrigins("*")
                //是否发送Cookie信息
                .allowCredentials(true)
                //放行哪些原始域(请求方式)
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                //放行哪些原始域(头部信息)
                .allowedHeaders("*")
                //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
                .exposedHeaders("token");*/
    }

    /**
     * 配置消息转换器
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 遍历所有转换器
        for (HttpMessageConverter<?> converter : converters) {
            // 对于JSON消息转换器，设置UTF-8编码
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
            }
        }
    }
}